using Gtk;
using System;

namespace TVListings
{
	public class AppearanceDialog : Gtk.Dialog
	{
		protected TreeIter Current;
		protected Gtk.ColorSelection ColorSelect;
		protected ProgramDisplay.ColorMapping[] Colors;
		protected Gtk.TreeStore CategoryStore;
		protected Gtk.TreeView CategoryView;

		public AppearanceDialog()
		{
			Stetic.Gui.Build(this, typeof(AppearanceDialog));
			Colors = UserSettings.GetSettings().Colors;
			CategoryStore = new TreeStore(typeof(string), typeof(Gdk.Color));
			CategoryStore.SetSortColumnId(0, Gtk.SortType.Ascending);

			CellRendererText cell = new CellRendererText();				
			TreeViewColumn column = new TreeViewColumn("Categories", cell, "text", 0);
			column.AddAttribute(cell, "background-gdk", 1);

			CategoryView.Model = CategoryStore;
			CategoryView.AppendColumn(column);
			CategoryView.Selection.Changed += new EventHandler(OnCategoryViewSelectionChanged);			
			
			foreach (ProgramDisplay.ColorMapping m in Colors)
				CategoryStore.AppendValues(m.Category, m.Color);
		}
		
		public void UseCategories(string[] Categories)
		{
			foreach (string c in Categories)
				CategoryStore.AppendValues(c, CategoryView.Style.Background(Gtk.StateType.Normal));
		}

		protected void OnCategoryViewSelectionChanged(object sender, System.EventArgs e)
		{
			if (CategoryStore.IterIsValid(Current))
				CategoryStore.SetValue(Current, 1, ColorSelect.CurrentColor);

			TreeIter iter;
			((TreeSelection)sender).GetSelected(out iter);
			if (!CategoryStore.IterIsValid(iter))
				return;
			
			/// TODO: set up picker with new color
			//ColorSelect.CurrentColor = (Gdk.Color)CategoryStore.GetValue(iter, 1);
		}

		protected virtual void OnSaveButtonClicked(object sender, System.EventArgs e)
		{
			/// TODO: save mappings
		
			this.Respond(ResponseType.Apply);
		}
	}
}
